const express = require("express");
const expressJoi = require("@escook/express-joi");

const registerRoutes = (router, moduleName) => {
	const { Menu } = require("../../router_handler/public/menu");
	const { Info, Update, ResetPwd, AvatarUpload } = require("../../router_handler/public/my");
	const { update_Schema, reset_schema } = require("../../schema/public/my");
	const { refreshToken, Code, Logout } = require("../../router_handler/public/login");
	const { email_schema } = require("../../schema/public/login");

	const routes = [
		{ method: "post", path: "/email", schema: email_schema, handler: Code },
		{ method: "get", path: "/refreshToken", schema: null, handler: refreshToken },
		{ method: "get", path: "/menu", schema: null, handler: Menu },
		{ method: "get", path: "/info", schema: null, handler: Info },
		{ method: "put", path: "/update", schema: update_Schema, handler: Update },
		{ method: "put", path: "/resetPwd", schema: reset_schema, handler: ResetPwd },
		{ method: "delete", path: "/logout", schema: null, handler: Logout }
	];

	routes.forEach(route => {
		const handlerWithTable = (req, res) => route.handler(req, res, moduleName);
		if (route.schema) {
			router[route.method](route.path, expressJoi(route.schema), handlerWithTable);
		} else {
			router[route.method](route.path, handlerWithTable);
		}
	});
};

module.exports = function createModuleRouter(moduleName) {
	const router = express.Router();
	registerRoutes(router, moduleName);
	return router;
};